package com.wanshi.servlet;

import com.alibaba.fastjson.JSON;
import com.wanshi.entity.Result;
import com.wanshi.entity.User;
import com.wanshi.service.CartService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/cart/delCartById")
public class delCartById extends HttpServlet {
    private CartService cartService = new CartService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1 获取jsp提交数据
        String prodid = request.getParameter("prodid");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        //2 操作数据库
        cartService.delCartById(user.getId()+"",prodid);

        //3 返回数据
        response.getWriter().write(JSON.toJSONString( Result.ok(null) ));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

